package com.sq.subject.domain.handler.subject;

import com.sq.subject.common.enums.IsDeletedFlagEnum;
import com.sq.subject.common.enums.SubjectInfoTypeEnum;
import com.sq.subject.domain.bo.SubjectAnswerBO;
import com.sq.subject.domain.bo.SubjectInfoBO;
import com.sq.subject.infra.basic.entity.SubjectRadio;
import com.sq.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 单选策略处理器
 * @author quan
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBo) {
        List<SubjectRadio> subjectRadioList = subjectInfoBo.getOptionList().stream().map(subjectAnswerBO -> {
            SubjectRadio subjectRadio = new SubjectRadio();
            subjectRadio.setSubjectId(subjectInfoBo.getSubjectId());
            subjectRadio.setOptionType(subjectAnswerBO.getOptionType());
            subjectRadio.setOptionContent(subjectAnswerBO.getOptionContent());
            subjectRadio.setIsCorrect(subjectAnswerBO.getIsCorrect());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            return subjectRadio;
        }).toList();
        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectInfoBO query(SubjectInfoBO subjectInfoBO) {
        SubjectRadio radio = new SubjectRadio();
        radio.setSubjectId(subjectInfoBO.getId());
        List<SubjectRadio> subjectRadioList = subjectRadioService.queryByCondition(radio);
        List<SubjectAnswerBO> subjectAnswerBOList = subjectRadioList.stream().map(subjectRadio -> {
            SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
            subjectAnswerBO.setOptionType(subjectRadio.getOptionType());
            subjectAnswerBO.setOptionContent(subjectRadio.getOptionContent());
            subjectAnswerBO.setIsCorrect(subjectRadio.getIsCorrect());
            return subjectAnswerBO;
        }).toList();
        subjectInfoBO.setOptionList(subjectAnswerBOList);
        return subjectInfoBO;
    }
}
